#!/bin/bash

installNVM() {
    nvmVersion=$(nvm -v)
    NVM_DIR="/home/${user}/dev/nvm"
    if [ -z "${nvmVersion}" ]; then
        echo "insstall"
        # NVM_DIR="/home/${user}/dev/nvm"
        git clone git://github.com/nvm-sh/nvm.git "$NVM_DIR"
        cd "$NVM_DIR"
        git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
        \. "$NVM_DIR/nvm.sh"

        sed -i "/NVM_DIR/d" /home/${user}/.bash_path
        echo "export NVM_DIR=$NVM_DIR" >>/home/${user}/.bash_path
        echo "[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\"  # This loads nvm" >>/home/${user}/.bash_path
        echo "[ -s \"\$NVM_DIR/bash_completion\" ] && \\. \"\$NVM_DIR/bash_completion\"  # This loads nvm bash_completion" >>/home/${user}/.bash_path
        source /home/${user}/.bash_path
    fi
    nvm -v
    echo "可选LTS版本"
    nvm ls-remote |grep "Latest LTS"
    read -p "选择要安装的版本 -1则退:" version
    if [ $version == "-1" ]; then
        echo "退出"
        return 0
    fi
    nvm install $version
    nvm use $version
    node -v
    npm -v
    npm config set registry https://registry.npm.taobao.org

    if [ -d "$NVM_DIR/versions/nod/$version/node_global_modules" ]; then 
        mkdir $NVM_DIR/versions/nod/$version/node_global_modules
    fi
    if [ -d "$NVM_DIR/node_cache" ]; then 
        mkdir $NVM_DIR/node_cache
    fi
    npm  config set prefix $NVM_DIR/node_global_modules
    npm  config set cache $NVM_DIR/node_cache

}

installNVM